package vashion.azeroth.back.member;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import vashion.azeroth.common.encrypt.Md5Encrypt;
import vashion.azeroth.contant.AzerothConstants.PersonalMember.MemberType;
import vashion.azeroth.core.manager.personal.PersonalMemberManager;
import vashion.azeroth.core.personal.pojo.PersonalMember;

@Controller
@RequestMapping("")
public class MemberController {
	
	@Autowired
	private PersonalMemberManager memberManager;

	@RequestMapping("login.htm")
	public String login(PersonalMember member, HttpServletResponse resp, HttpSession httpSession, RedirectAttributes attr) throws IOException {
		
		
		PersonalMember dbmember = memberManager.getMemberByName(member.getMemberName());
		
		if(null == dbmember) {
			attr.addFlashAttribute("loginMsg", "用户名或密码不正确");
			return "redirect:tologin.htm";
		}
		
		if(!Md5Encrypt.encrypt(member.getMemberPwd(), "UTF-8").equals(dbmember.getMemberPwd())) {
			attr.addFlashAttribute("loginMsg", "用户名或密码不正确");
			return "redirect:tologin.htm";
		}
		
		if(!dbmember.getType().equals(MemberType.supplier)) {
			attr.addFlashAttribute("loginMsg", "用户名或密码不正确");
			return "redirect:tologin.htm";
		}
		
		httpSession.setAttribute("member", member);
		
		return "forward:success.htm";
	}
	
	@RequestMapping("tologin.htm")
	public String tologin(){
		return "/member/login";
	}
	
	@RequestMapping("success.htm")
	public String success(){
		return "/order/disburseOrders";
	}
	
	@RequestMapping("loginOut.htm")
	public String loginOut(HttpServletResponse resp, HttpSession httpSession) throws IOException {

		httpSession.removeAttribute("member");
		
		return "redirect:tologin.htm";
	}
}
